package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.domain.Appmanager;
import com.ruoyi.system.domain.Drtypeinfo;
import com.ruoyi.system.domain.Sub;
import com.ruoyi.system.service.IAppmanagerService;
import com.ruoyi.system.service.ISubService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.GlobalDrTypeInfo;
import com.ruoyi.system.service.IGlobalDrTypeInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 全局设备类型Controller
 * 
 * @author yaozh
 * @date 2024-03-16
 */
@RestController
@RequestMapping("/system/drtypeinfoglobal")
public class GlobalDrTypeInfoController extends BaseController
{
    @Resource
    private IGlobalDrTypeInfoService globalDrTypeInfoService;
    @Resource
    private IAppmanagerService appManagerService;
    @Resource
    private ISubService subService;

    /**
     * 查询全局设备类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:drtypeinfoglobal:list')")
    @GetMapping("/list")
    public AjaxResult list(GlobalDrTypeInfo globalDrTypeInfo)
    {
        List<GlobalDrTypeInfo> list = globalDrTypeInfoService.selectGlobalDrTypeInfoList(globalDrTypeInfo);
        return success(list);
    }

    /**
     * 导出全局设备类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:drtypeinfoglobal:export')")
    @Log(title = "全局设备类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, GlobalDrTypeInfo globalDrTypeInfo)
    {
        List<GlobalDrTypeInfo> list = globalDrTypeInfoService.selectGlobalDrTypeInfoList(globalDrTypeInfo);
        ExcelUtil<GlobalDrTypeInfo> util = new ExcelUtil<GlobalDrTypeInfo>(GlobalDrTypeInfo.class);
        util.exportExcel(response, list, "全局设备类型数据");
    }

    /**
     * 获取全局设备类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:drtypeinfoglobal:query')")
    @GetMapping(value = "/{drtypeid}")
    public AjaxResult getInfo(@PathVariable("drtypeid") Long drtypeid)
    {
        return success(globalDrTypeInfoService.selectGlobalDrTypeInfoByDrtypeid(drtypeid));
    }

    /**
     * 新增全局设备类型
     */
    @PreAuthorize("@ss.hasPermi('system:drtypeinfoglobal:add')")
    @Log(title = "全局设备类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GlobalDrTypeInfo globalDrTypeInfo)
    {
        return toAjax(globalDrTypeInfoService.insertGlobalDrTypeInfo(globalDrTypeInfo));
    }

    /**
     * 修改全局设备类型
     */
    @PreAuthorize("@ss.hasPermi('system:drtypeinfoglobal:edit')")
    @Log(title = "全局设备类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GlobalDrTypeInfo globalDrTypeInfo)
    {
        return toAjax(globalDrTypeInfoService.updateGlobalDrTypeInfo(globalDrTypeInfo));
    }

    /**
     * 删除全局设备类型
     */
    @PreAuthorize("@ss.hasPermi('system:drtypeinfoglobal:remove')")
    @Log(title = "全局设备类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{drtypeids}")
    public AjaxResult remove(@PathVariable Long[] drtypeids)
    {
        boolean isAdmin = getLoginUser().getUser().isAdmin();
        if(!isAdmin){
            String message = "拒绝操作，仅超级管理员权限才能删除！";
            throw new RuntimeException(message);
        }
        return toAjax(globalDrTypeInfoService.deleteGlobalDrTypeInfoByDrtypeids(drtypeids));
    }

    /**
     * 获取设备类型树列表
     */
    @PreAuthorize("@ss.hasPermi('system:drtypeinfoglobal:list')")
    @GetMapping("/drTypeTree")
    public AjaxResult drTypeTree(GlobalDrTypeInfo globalDrTypeInfo)
    {
        return success(globalDrTypeInfoService.selectDrTypeTreeList(globalDrTypeInfo));
    }

    @PreAuthorize("@ss.hasPermi('system:drtypeinfoglobal:list')")
    @GetMapping("/subAllList")
    public AjaxResult subAllList()
    {
        /*List<Appmanager> appList = appManagerService.selectAppManagerListByUserAndDbName(getLoginUser());
        if(appList == null || appList.size() == 0){
            return success(new ArrayList<>());
        }
        return success(subService.selectSubList(new Sub()));*/

        return success(new ArrayList<>());
    }
}
